<?php

declare(strict_types=1);
/**
 * This file is part of Hyper f.
 *
 * @document https://console-docs.apipost.cn/preview/2d9b8414ff1e50d7/789d1eb1f5a36f86
 * @author   howe <howe1995@163.com>
 *
 */
// print_r($argv);

// $command = "./server.sh {$argv[1]}";
// $output = shell_exec($command);
// echo 'Output: ' . $output;


const APP_NAME = 'hblog';
const APP_DIR = 'hblog-hyperf';

if (!isset($argv[1]) || $argv[1] === 'help') {
    echo "Usage: php dev.php <watch|start|stop>" . PHP_EOL;
    echo "Available commands:" . PHP_EOL;
    echo "  watch  - Watch for changes and restart the server" . PHP_EOL;
    echo "  start  - Start the server" . PHP_EOL;
    echo "  stop   - Stop the server" . PHP_EOL;
    exit();
}

// “grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。
// “awk '{print $1}'”打印（print）出第一列的内容，而这正好是进程号PID。
// “xargs kill -s 9”中的xargs命令是用来把前面命令的输出结果（PID）作为“kill -s 9”命令的参数，并执行该命令。“kill -s 9”会强行杀掉指定进程。
// echo(shell_exec("ps | grep bin/hyperf.php | grep -v grep | awk '{print $1}' | xargs kill -9"));
// echo(shell_exec("ps | grep tamgo | grep -v grep | awk '{print $1}' | xargs kill -9"));

// pkill＝pgrep+kill
// echo shell_exec('pkill -9 php');
// echo '删除 php 进程' . PHP_EOL;
// exec('rm -rf runtime/container');
// echo '删除 runtime/container 代理文件' . PHP_EOL;

// 定义颜色
$red = "\033[31m";
$green = "\033[32m";
$yellow = "\033[33m";
$blue = "\033[34m";
$reset = "\033[0m";
// 使用颜色输出
// echo "这是红色的文本：" . $red . "红色文字" . $reset . "\n";
// echo "这是绿色的文本：" . $green . "绿色文字" . $reset . "\n";
// echo "这是黄色的文本：" . $yellow . "黄色文字" . $reset . "\n";
// echo "这是蓝色的文本：" . $blue . "蓝色文字" . $reset . "\n";
// exit();
$beginTime = microtime(true);
$cmd = $argv[1];
if ($cmd === 'watch') {
    echo "{$green}开始监控文件变化，自动重启服务...{$reset}" . PHP_EOL;
    $command = 'php ../' . APP_DIR . '/bin/hyperf.php server:watch';
} elseif ($cmd === 'start') {
    echo "{$green}开始启动服务...{$reset}" . PHP_EOL;
    $command = 'php ../' . APP_DIR . '/bin/hyperf.php start';
} elseif ($cmd === 'stop') {
    echo "{$yellow}开始停止服务...{$reset}" . PHP_EOL;
    $command = 'pkill -f "../' . APP_DIR . '/bin/hyperf.php server:watch" & pkill -f "../' . APP_DIR . '/bin/hyperf.php start"  & pkill -9 ' . APP_NAME . '.Master';
} else {
    echo "Invalid command: $cmd" . PHP_EOL;
    echo "Usage: php dev.php <watch|start|stop>" . PHP_EOL;
    echo "Available commands:" . PHP_EOL;
    echo "  watch  - Watch for changes and restart the server" . PHP_EOL;
    echo "  start  - Start the server" . PHP_EOL;
    echo "  stop   - Stop the server" . PHP_EOL;
    exit();
}

// rm -rf runtime/container
exec('rm -rf runtime/container', $output1, $returnVar1);
// echo '删除 runtime/container 代理文件' . PHP_EOL;
if ($returnVar1 === 0) {
    echo "{$red}删除 runtime/container 代理文件成功{$reset}" . PHP_EOL;
} else {
    echo "{$red}删除 runtime/container 代理文件失败，返回码: {$reset}" . $returnVar1 . PHP_EOL;
}
$handle = popen($command, 'r');
if ($handle) {
    // 设置流为非阻塞模式
    // stream_set_blocking($handle, false);
    while (!feof($handle)) {
        $output = fgets($handle);
        if ($output !== false) {
            echo $output;
            $endTime = microtime(true);
            echo "{$green}启动耗时: " . $endTime - $beginTime . " 秒{$reset}" . PHP_EOL;
            flush(); // 确保输出立即发送
        }
    }
    pclose($handle);
}
echo "{$red}commands finished{$reset}" . PHP_EOL;
// 记录结束时间
